/**
 * Created by Administrator on 2017/7/22.
 */
    //所有规则中的myCheckCards.cards都是经过按compareValue升序排序的
var GanDengYan = require('./GanDengYan');
GanDengYan.rule_danPai = function(myCheckCards,rValue)
{
    if(1 != myCheckCards.cardsCount)
    {
        return;
    }
    if(0 == myCheckCards.laiziNum)
    {
        myCheckCards.type = rValue;
        this._addResult(myCheckCards);
    }
},
GanDengYan.rule_duiZi = function(myCheckCards,rValue)
{
    if(2 != myCheckCards.cardsCount)
    {
        return;
    }
    if(2 != myCheckCards.laiziNum)
    {
        var cards = myCheckCards.cards;
        var isMatch = false;
        if(1 == myCheckCards.laiziNum)
        {
            //console.log("癞对子");
            cards[0].replaceValue = cards[1].originValue;
            isMatch = true;
        }
        else if(0 == myCheckCards.laiziNum)
        {
            if(cards[0].id == cards[1].id)
            {
                //console.log("普通对子");
                isMatch = true;
            }
        }
        if(isMatch)
        {
            myCheckCards.type = rValue;
            this._addResult(myCheckCards);
        }
    }
},
GanDengYan.rule_shuangLaiZha = function(myCheckCards,rValue)
{
    if(2 != myCheckCards.cardsCount)
    {
        return;
    }
    if(2 == myCheckCards.laiziNum)
    {
        //console.log("双癞炸");
        myCheckCards.type = rValue;
        this._addResult(myCheckCards);
    }
},
GanDengYan.rule_sanLaiZha = function(myCheckCards,rValue)
{
    if(3 != myCheckCards.cardsCount)
    {
        return;
    }
    if(3 == myCheckCards.laiziNum)
    {
        //console.log("三癞炸");
        myCheckCards.type = rValue;
        this._addResult(myCheckCards);
    }
},
GanDengYan.rule_sanZhangZha = function(myCheckCards,rValue)
{
    this.rule_zhaDan(myCheckCards,rValue);
},
GanDengYan.rule_siZhangZha = function(myCheckCards,rValue)
{
    this.rule_zhaDan(myCheckCards,rValue);
},
GanDengYan.rule_wuZhangZha = function(myCheckCards,rValue)
{
    this.rule_zhaDan(myCheckCards,rValue);
},
GanDengYan.rule_liuZhangZha = function(myCheckCards,rValue)
{
    this.rule_zhaDan(myCheckCards,rValue);
},

//以下是任意张数量的牌型规则

//检测顺子，适用于任意张数量的牌
GanDengYan.rule_shunZi = function(myCheckCards,rValue)
{
    if(myCheckCards.cardsCount < this.minCardsNumWithStraight || 1 == myCheckCards.cardNum)
    {
        return;
    }
    var cards = myCheckCards.cards;
    var i = 0;
    var k = 0;
    var suffix = 0;
    var count = 0;  //有效牌数量
    var cardsValue = this._copyCardsValue();
    var checkShunZiNum = myCheckCards.cardsCount;
    var backupCheckCards = null;
    for(i = 0; i < cardsValue.length; ++i)
    {
        for(k = 0; k < cards.length; ++k)
        {
            if(cardsValue[i].compareValue == cards[k].compareValue)
            {
                //console.log("牌" + cardsValue[i].compareValue + "].originValue = " + cardsValue[i].originValue);
                cardsValue[i].num++;
                if(cardsValue[i].num > 1)
                {
                    return;
                }
            }
        }
    }
    //检测有效牌数量
    for(i = 0 ; i <= cardsValue.length - checkShunZiNum; ++i)
    {
        count = 0;
        backupCheckCards = this._copyCheckCards(myCheckCards);
        cards = backupCheckCards.cards;
        for(k = i ; k < i + checkShunZiNum; ++k)
        {
            count += cardsValue[k].num;
        }
        //console.log("当前检测到 " + cardsValue[i].compareValue);
        //console.log("有效牌数量 = " + count);
        if(count + backupCheckCards.laiziNum == backupCheckCards.cardsCount)
        {
            suffix = 0;
            for(k = i ; k < i + checkShunZiNum; ++k)
            {
                if(cardsValue[k].num == 0)
                {
                    cards[suffix++].replaceValue = cardsValue[k].originValue;
                }
                if(suffix > backupCheckCards.laiziNum)
                {
                    break;
                }
            }
            //console.log("顺子");
            backupCheckCards.type = rValue;
            this._addResult(backupCheckCards);
        }
    }
};
//检测连对，适用于任意张数量的牌
GanDengYan.rule_lianDui = function(myCheckCards,rValue)
{
    if(1 == myCheckCards.cardsCount % 2 || myCheckCards.cardsCount / 2 < this.minStrPairNum)
    {
        return;
    }
    var cards = myCheckCards.cards;
    var i = 0;
    var k = 0;
    var suffix = 0;
    var count = 0;
    var backupCheckCards = null;
    var cardsValue = this._copyCardsValue();
    var checkLianDui = myCheckCards.cardsCount / 2;   //检测连对的数量
    for(i = 0; i < cardsValue.length; ++i)
    {
        for(k = 0; k < cards.length; ++k)
        {
            if(cardsValue[i].compareValue == cards[k].compareValue)
            {
                cardsValue[i].num++;
                if(cardsValue[i].num > 2)
                {
                    return;
                }
            }
        }
    }
    for(i = 0 ; i <= cardsValue.length - checkLianDui; ++i)
    {
        count = 0;
        for(k = i; k < i + checkLianDui; ++k)
        {
            count += cardsValue[k].num;
        }
        //console.log("当前检测到 " + cardsValue[i].compareValue);
        //console.log("有效牌数量 = " + count);
        if(myCheckCards.laiziNum + count == myCheckCards.cardsCount)
        {
            suffix = 0;
            backupCheckCards = this._copyCheckCards(myCheckCards);
            cards = backupCheckCards.cards;
            for(k = i; k < i + checkLianDui; ++k)
            {
                if(0 == cardsValue[k].num)
                {
                    cards[suffix++].replaceValue = cardsValue[k].originValue;
                    cards[suffix++].replaceValue = cardsValue[k].originValue;
                }
                else if(1 == cardsValue[k].num)
                {
                    cards[suffix++].replaceValue = cardsValue[k].originValue;
                }
                if(suffix >= backupCheckCards.laiziNum)
                {
                    break;
                }
            }
            //console.log("连对");
            backupCheckCards.type = rValue;
            this._addResult(backupCheckCards);
        }
    }
};
//检测纯癞子炸弹，适用于任意癞子数量的牌
GanDengYan.rule_laiziZha = function(myCheckCards,rValue)
{
    if(myCheckCards.laiziNum == myCheckCards.cardsCount)
    {
        //console.log("纯癞子炸");
        myCheckCards.type = rValue;
        this._addResult(myCheckCards);
    }
};
//检测非纯癞子炸弹，适用于任意癞子数量的牌
GanDengYan.rule_zhaDan = function(myCheckCards,rValue)
{
    if(0 == myCheckCards.cardNum || myCheckCards.cardNum > 4)
    {
        return;
    }
    var cards = myCheckCards.cards;
    var isMatch = true;
    var refCard = cards[cards.length - 1];
    var i = 0;
    for(i = cards.length - 2; i >= myCheckCards.laiziNum; --i)
    {
        if(cards[i].compareValue != refCard.compareValue)
        {
            isMatch = false;
            break;
        }
    }
    if(isMatch)
    {
        for(i = 0; i < myCheckCards.laiziNum; ++i)
        {
            cards[i].replaceValue = refCard.originValue;
        }
        //console.log("炸弹");
        myCheckCards.type = rValue;
        this._addResult(myCheckCards);
    }
};



//以下是校验规则
GanDengYan.verify_shunZi = function(verifiedCards,rValue)
{
    verifiedCards.type = 0;
    var isMatch = true;
    var cards = verifiedCards.cards;
    var refCard = cards[0];
    for(var i = 1; i < cards.length; ++i)
    {
        if(cards[i].compareValue == refCard.compareValue)
        {
            isMatch = false;
            break;
        }
    }
    if(isMatch)
    {
        console.log("顺子");
        verifiedCards.type = rValue;
    }
};

GanDengYan.verify_lianDui = function(verifiedCards,rValue)
{
    if(0 != verifiedCards.cardsCount % 2 || verifiedCards.cardsCount < this.minStrPairNum * 2)
    {
        verifiedCards.type = 0;
        return;
    }
    var isMatch = true;
    var cards = verifiedCards.cards;
    var refCard = cards[0];
    for(var i = 0; i < cards.length; i += 2)
    {
        if(cards[i].compareValue != cards[i + 1].compareValue)
        {
            isMatch = false;
            break;
        }
        if(i > 1)
        {
            if(1 != cards[i].compareValue - refCard.compareValue)
            {
                isMatch = false;
                break;
            }
            refCard = cards[i];
        }
    }
    if(isMatch)
    {
        console.log("连对");
        verifiedCards.type = rValue;
    }
    else
    {
        verifiedCards.type = 0;
    }
};

GanDengYan.verify_danPai = function(verifiedCards,rValue)
{
    if(1 == verifiedCards.cardsCount && 1 != verifiedCards.laiziNum)
    {
        console.log("单牌");
        verifiedCards.type = rValue;
    }
    else
    {
        verifiedCards.type = 0;
    }
};

GanDengYan.verify_duiZi = function(verifiedCards,rValue)
{
    if(2 == verifiedCards.cardsCount && 2 != verifiedCards.laiziNum
        && (verifiedCards.cards[0].compareValue == verifiedCards.cards[1].compareValue))
    {
        console.log("对子");
        verifiedCards.type = rValue;
    }
    else
    {
        verifiedCards.type = 0;
    }
};
GanDengYan.verify_laiziZha = function(verifiedCards,rValue)
{
    if(verifiedCards.laiziNum == verifiedCards.cardsCount)
    {
        console.log("纯癞子炸");
        verifiedCards.type = rValue;
    }
    else
    {
        verifiedCards.type = 0;
    }
};

GanDengYan.verify_zhaDan = function(verifiedCards,rValue)
{
    verifiedCards.type = 0;
    if(verifiedCards.laiziNum != verifiedCards.cardsCount)
    {
        var isMatch = true;
        var cards = verifiedCards.cards;
        var refCard = cards[0];
        for(var i = 1; i < cards.length; ++i)
        {
            if(cards[i].compareValue != refCard.compareValue)
            {
                isMatch = false;
                break;
            }
        }
        if(isMatch)
        {
            console.log("炸弹");
            verifiedCards.type = rValue;
        }
    }
};

GanDengYan.verify_shuangLaiZha = function(verifiedCards,rValue)
{
    this.verify_laiziZha(verifiedCards,rValue);
};

GanDengYan.verify_sanLaiZha = function(verifiedCards,rValue)
{
    this.verify_laiziZha(verifiedCards,rValue);
};

GanDengYan.verify_sanZhangZha = function(verifiedCards,rValue)
{
    this.verify_zhaDan(verifiedCards,rValue);
};

GanDengYan.verify_siZhangZha = function(verifiedCards,rValue)
{
    this.verify_zhaDan(verifiedCards,rValue);
};

GanDengYan.verify_wuZhangZha = function(verifiedCards,rValue)
{
    this.verify_zhaDan(verifiedCards,rValue);
};

GanDengYan.verify_liuZhangZha = function(verifiedCards,rValue)
{
    this.verify_zhaDan(verifiedCards,rValue);
};

module.exports = GanDengYan;